/* Weibull coup attempts model */

set more off

set mem 100m

use "c:\Data Sets\Africa\africa_1.dta", clear
stset coupcount, failure(atcoup) id(coupid)
streg miltogdp currenta  intrimgv subsh num_coal seatlar pmpty_s natun civ_war polity anarchy nxtpa_el us_aid sunus gdpgrowt populati lagpcgdp imfstatu Wgrow WoverS, dist(w) cluster(code) 

mfx compute

matrix b1=e(Xmfx_dydx)
matrix v1=e(Xmfx_se_dydx)
save "c:\Data Sets\Africa\africa_1.dta", replace

use "c:\Data Sets\Africa\africa_2.dta", clear
stset coupcount, failure(atcoup) id(coupid)
streg miltogdp currenta  intrimgv subsh num_coal seatlar pmpty_s natun civ_war polity anarchy nxtpa_el us_aid sunus gdpgrowt populati lagpcgdp imfstatu Wgrow WoverS, dist(w) cluster(code) 

mfx compute

matrix b2=e(Xmfx_dydx)
matrix v2=e(Xmfx_se_dydx)
save "c:\Data Sets\Africa\africa_2.dta", replace

use "c:\Data Sets\Africa\africa_3.dta", clear
stset coupcount, failure(atcoup) id(coupid)
streg miltogdp currenta  intrimgv subsh num_coal seatlar pmpty_s natun civ_war polity anarchy nxtpa_el us_aid sunus gdpgrowt populati lagpcgdp imfstatu Wgrow WoverS, dist(w) cluster(code) 

mfx compute

matrix b3=e(Xmfx_dydx)
matrix v3=e(Xmfx_se_dydx)
save "c:\Data Sets\Africa\africa_3.dta", replace

use "c:\Data Sets\Africa\africa_4.dta", clear
stset coupcount, failure(atcoup) id(coupid)
streg miltogdp currenta  intrimgv subsh num_coal seatlar pmpty_s natun civ_war polity anarchy nxtpa_el us_aid sunus gdpgrowt populati lagpcgdp imfstatu Wgrow WoverS, dist(w) cluster(code) 

mfx compute

matrix b4=e(Xmfx_dydx)
matrix v4=e(Xmfx_se_dydx)
save "c:\Data Sets\Africa\africa_4.dta", replace

use "c:\Data Sets\Africa\africa_5.dta", clear
stset coupcount, failure(atcoup) id(coupid)
streg miltogdp currenta  intrimgv subsh num_coal seatlar pmpty_s natun civ_war polity anarchy nxtpa_el us_aid sunus gdpgrowt populati lagpcgdp imfstatu Wgrow WoverS, dist(w) cluster(code) 

mfx compute

matrix b5=e(Xmfx_dydx)
matrix v5=e(Xmfx_se_dydx)
save "c:\Data Sets\Africa\africa_5.dta", replace

use "c:\Data Sets\Africa\africa_6.dta", clear
stset coupcount, failure(atcoup) id(coupid)
streg miltogdp currenta  intrimgv subsh num_coal seatlar pmpty_s natun civ_war polity anarchy nxtpa_el us_aid sunus gdpgrowt populati lagpcgdp imfstatu Wgrow WoverS, dist(w) cluster(code) 

mfx compute

matrix b6=e(Xmfx_dydx)
matrix v6=e(Xmfx_se_dydx)
save "c:\Data Sets\Africa\africa_6.dta", replace

use "c:\Data Sets\Africa\africa_7.dta", clear
stset coupcount, failure(atcoup) id(coupid)
streg miltogdp currenta  intrimgv subsh num_coal seatlar pmpty_s natun civ_war polity anarchy nxtpa_el us_aid sunus gdpgrowt populati lagpcgdp imfstatu Wgrow WoverS, dist(w) cluster(code) 

mfx compute

matrix b7=e(Xmfx_dydx)
matrix v7=e(Xmfx_se_dydx)
save "c:\Data Sets\Africa\africa_7.dta", replace

use "c:\Data Sets\Africa\africa_8.dta", clear
stset coupcount, failure(atcoup) id(coupid)
streg miltogdp currenta  intrimgv subsh num_coal seatlar pmpty_s natun civ_war polity anarchy nxtpa_el us_aid sunus gdpgrowt populati lagpcgdp imfstatu Wgrow WoverS, dist(w) cluster(code) 

mfx compute

matrix b8=e(Xmfx_dydx)
matrix v8=e(Xmfx_se_dydx)
save "c:\Data Sets\Africa\africa_8.dta", replace

use "c:\Data Sets\Africa\africa_9.dta", clear
stset coupcount, failure(atcoup) id(coupid)
streg miltogdp currenta  intrimgv subsh num_coal seatlar pmpty_s natun civ_war polity anarchy nxtpa_el us_aid sunus gdpgrowt populati lagpcgdp imfstatu Wgrow WoverS, dist(w) cluster(code) 

mfx compute

matrix b9=e(Xmfx_dydx)
matrix v9=e(Xmfx_se_dydx)
save "c:\Data Sets\Africa\africa_9.dta", replace

use "c:\Data Sets\Africa\africa_10.dta", clear
stset coupcount, failure(atcoup) id(coupid)
streg miltogdp currenta  intrimgv subsh num_coal seatlar pmpty_s natun civ_war polity anarchy nxtpa_el us_aid sunus gdpgrowt populati lagpcgdp imfstatu Wgrow WoverS, dist(w) cluster(code) 

mfx compute

matrix b10=e(Xmfx_dydx)
matrix v10=e(Xmfx_se_dydx)
save "c:\Data Sets\Africa\africa_10.dta", replace

/* Marginal effects, full model */

matrix B4=b1\b2\b3\b4\b5\b6\b7\b8\b9\b10
matrix row1=[1,1,1,1,1,1,1,1,1,1]
matrix B4mi=(1/10)*([b1\b2\b3\b4\b5\b6\b7\b8\b9\b10]'*row1')

mat V4mi=(v1+v2+v3+v4+v5+v6+v7+v8+v9+v10)/10
mat Sqs=B4-row1'*B4mi'
mat A=Sqs[1, 1...]
mat AA=diag(A)
mat B=Sqs[2, 1...]
mat BB=diag(B)
mat C=Sqs[3, 1...]
mat CC=diag(C)
mat D=Sqs[4, 1...]
mat DD=diag(D)
mat E=Sqs[5, 1...]
mat EE=diag(E)
mat F=Sqs[6, 1...]
mat FF=diag(F)
mat G=Sqs[7, 1...]
mat GG=diag(G)
mat H=Sqs[8, 1...]
mat HH=diag(H)
mat J=Sqs[9, 1...]
mat JJ=diag(J)
mat K=Sqs[10, 1...]
mat KK=diag(K)

mat Sqsq=[A*AA\B*BB\C*CC\D*DD\E*EE\F*FF\G*GG\H*HH\J*JJ\K*KK]'*row1'

mat SE4sq=(vecdiag(cholesky(diag(Sqsq))))'
mat SE4mi=V4mi'+(1/9)*SE4sq*(11/10)

mat final1=[B4mi, SE4mi]
mat colnames final1=Coeff StErr
noisily mat list final1

